/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ListViewAdaptWidth.java   
 * @Package com.jiyi.widget   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2018-1-9 下午7:30:03   
 * @version V1.0     
 */  
package com.jiyi.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

/**   
 * @ClassName:  ListViewAdaptWidth   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: HYM  
 * @date:   2018-1-9 下午7:30:03   
 *      
 */
public class ListViewAdaptWidth extends ListView {  
	  
    public ListViewAdaptWidth(Context context) {  
        super(context);  
    }  
  
    public ListViewAdaptWidth(Context context, AttributeSet attrs, int defStyleAttr) {  
        super(context, attrs, defStyleAttr);  
    }  
  
    public ListViewAdaptWidth(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        int width = getMaxWidthOfChildren() + getPaddingLeft() + getPaddingRight();//计算listview的宽度  
        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), heightMeasureSpec);//设置listview的宽高  
  
    }  
  
    /** 
     * 计算item的最大宽度 
     * 
     * @return 
     */  
    private int getMaxWidthOfChildren() {  
        int maxWidth = 0;  
        View view = null;  
        int count = getAdapter().getCount();  
        for (int i = 0; i < count; i++) {  
            view = getAdapter().getView(i, view, this);  
            view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);  
            if (view.getMeasuredWidth() > maxWidth)  
                maxWidth = view.getMeasuredWidth();  
        }  
        return maxWidth;  
    }  
}  
